﻿namespace FasadPattern.Composits
{
    using System;
    using System.Collections.Generic;
    using FasadPattern.SerializeWorker;
    using FasadPattern.SerializeWorker.JSONObjects;

    public class Person : IAssignable
    {
        #region Constructors

        /// <summary>
        /// Initializes new Person without params
        /// </summary>
        public Person()
            : this(0, string.Empty, string.Empty, 0)
        {
        }

        /// <summary>
        /// Initializes new Person without adresses and contacts
        /// </summary>
        /// <param name="id">Person ID</param>
        /// <param name="fname">Name</param>
        /// <param name="lname">Surname</param>
        /// <param name="age">Age</param>
        public Person(int id, string fname, string lname, int age)
            : this(id, fname, lname, age, new List<Adress>(), new List<Contact>())
        {
        }

        /// <summary>
        /// Initializes new Person 
        /// </summary>
        /// <param name="id">Person ID</param>
        /// <param name="fname">Name</param>
        /// <param name="lname">Surname</param>
        /// <param name="age">Age</param>
        /// <param name="adressList">Adresses</param>
        /// <param name="contacts">Contacts</param>
        public Person(int id, string fname, string lname, int age, List<Adress> adressList, List<Contact> contacts)
        {
            this.ID = id;
            this.FirstName = fname;
            this.LastName = lname;
            this.Age = age;
            this.AdressList = adressList;
            this.Contacts = contacts;
        }

        #endregion Constructors

        #region Properties

        public int ID { get; private set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int Age { get; set; }

        public List<Adress> AdressList { get; set; }

        public List<Contact> Contacts { get; set; }

        #endregion Properties

        #region IAssignable implementation

        /// <summary>
        /// Transfares content
        /// </summary>
        /// <param name="obj">Object to transfare</param>
        public void AssignTo(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();

            if (obj is Person)
            {
                Person p = obj as Person;
                p.AdressList = this.AdressList;
                p.Age = this.Age;
                p.Contacts = this.Contacts;
                p.FirstName = this.FirstName;
                p.ID = this.ID;
                p.LastName = this.LastName;

                return;
            }

            if (obj is JSONObject)
            {
                JSONObject result = obj as JSONObject;
                JSONSerializer ser = new JSONSerializer();
                result.EncodedStreamList = ser.Serialize(this).EncodedStreamList;

                return;
            }

            throw new NotSupportedException();
        }

        #endregion
    }
}
